package top.eggcode.store;

import org.springframework.stereotype.Repository;
import top.eggcode.model.App;
import top.eggcode.model.AppQuery;
import top.eggcode.model.AppView;
import top.eggcode.component.query.CriteriaBuilder;
import top.eggcode.component.query.model.PageList;
import top.eggcode.base.JpaSimpleStore;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import java.util.List;

@Repository
public class AppStore extends JpaSimpleStore<App, String> {

    @Resource
    private CriteriaBuilder builder;

    public AppStore(EntityManager manager) {
        super(App.class, manager);
    }

    public List<App> findAll() {
        return builder.createQuery(App.class)
                .fromEntity()
                .where(cond -> {
                    cond.eq("deleted", 0);
                })
                .fetch();
    }

    public PageList<AppView> searchList(AppQuery query) {
        return builder.createQuery(AppView.class)
                .select("*")
                .from("arm_app", "root")
                .where(cond -> {
                    if (query.getName() != null) {
                        cond.likeAny("root.name", query.getName());
                    }
                    cond.eq("deleted", 0);
                })
                .fetchPage(query);
    }


}
